//
//  MyMovieVC.m
//  UI豆瓣2
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "MyMovieVC.h"
#import "MyMovieCell.h"
#import "Movie.h"
#import "MovieDetailViewController.h"
#import "DataBaseCollect.h"
#import "LoginViewController.h"
@interface MyMovieVC ()<UITableViewDelegate,UITableViewDataSource,MyMovieDelegate>

@property (nonatomic, retain)UITableView *myMovieTV;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)NSMutableArray *arr;   //存放被勾选的电影的数组
@property (nonatomic, retain)NSMutableArray *indexA;

@end

@implementation MyMovieVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.arr = [NSMutableArray array];
    self.indexA = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    self.array = [NSMutableArray array];
    self.myMovieTV = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    self.myMovieTV.delegate = self;
    self.myMovieTV.dataSource = self;
    [self.view addSubview:self.myMovieTV];
    
    [[DataBaseCollect shareDataBase] openDB];
    [[DataBaseCollect shareDataBase] createTable];
    NSUserDefaults *loginUserDef = [NSUserDefaults standardUserDefaults];
    NSString *name = [loginUserDef stringForKey:@"name"];
//    NSLog(@"当前用户名name====== %@", name);
    self.array = [[DataBaseCollect shareDataBase] selectMovieArray:name];
//    NSLog(@"array = %@",self.array);
    //去掉多余cell
    self.myMovieTV.tableFooterView = [UIView new];
    //去掉cell分割线
    self.myMovieTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [_myMovieTV release];
    
    self.navigationItem.title = @"我的电影";
    
    [self.myMovieTV registerClass:[MyMovieCell class] forCellReuseIdentifier:@"myCell"];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(deleAction:)];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"myCell";
    MyMovieCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    Movie *movie = [self.array objectAtIndex:indexPath.row];
    
    cell.movie = movie;
    cell.indexPath = indexPath;
    cell.myMovieDele = self;
    return cell;
}
- (void)info:(Movie *)movie indexPath:(NSIndexPath *)index{
    
    
    if (movie.selected) {
        //当电影被勾选,加到
        [self.arr addObject:movie.movieName];
        [self.indexA addObject:index];
    }else{
        [self.arr removeObject:movie.movieName];
        [self.indexA removeObject:index];
    }
    NSLog(@"arr ===== %@", self.arr);
    
    NSLog(@"movieName = %@, movieId = %@", movie.movieName, movie.movieId);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MovieDetailViewController *movieDetilVC = [[MovieDetailViewController alloc] init];
    Movie *movie = [self.array objectAtIndex:indexPath.row];
    
    movieDetilVC.movieID = movie.movieId;
    [self.navigationController pushViewController:movieDetilVC animated:YES];
    [movieDetilVC release];
    
}
#pragma mark -知识点  询问哪行需要编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    //返回值为yes,每行都要编辑
    return YES;
}

#pragma mark -知识点  设置哪行的编辑状态(删除/添加)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    //每行都是删除状态
    return UITableViewCellEditingStyleDelete;
}

#pragma mark -知识点  实现编辑逻辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    Movie *movie = [self.array objectAtIndex:indexPath.row];
    
    NSString *name = movie.movieName;
    NSLog(@"************name = %@", name);
    NSUserDefaults *loginUserDef = [NSUserDefaults standardUserDefaults];
    //登录时,取出当前的用户名是多少
    NSString *userName = [loginUserDef stringForKey:@"name"];
    
    //根据电影名删除数据库里的数据
    [[DataBaseCollect shareDataBase]deleteMovie:name user:userName];
    //删除
    NSLog(@"indexPath-----%@", indexPath);
    [self.array removeObjectAtIndex:indexPath.row];
    //删除cell
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationRight];
    
    [self.myMovieTV reloadData];
}

//设置删除按钮文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

- (void)deleAction:(UIBarButtonItem *)btn{
    
    for (int i = 0; i < self.arr.count; i++) {
        NSString *name = [self.arr objectAtIndex:i];
        NSUserDefaults *loginUserDef = [NSUserDefaults standardUserDefaults];
        //登录时,取出当前的用户名是多少
        NSString *userName = [loginUserDef stringForKey:@"name"];
        [[DataBaseCollect shareDataBase] deleteMovie:name user:userName];
        NSIndexPath *temp = [self.indexA objectAtIndex:i];
        NSLog(@"indexpath = %@", temp);
//        [self.myMovieTV deleteRowsAtIndexPaths:[NSArray arrayWithObjects:temp, nil] withRowAnimation:UITableViewRowAnimationRight];
        [self.myMovieTV reloadData];
    }
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
